package com.example.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manager.domain.R;
import com.example.manager.domain.SysPost;
import com.example.manager.domain.SysUser;
import com.example.manager.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//岗位表CRUD
@RequestMapping("/slm/posts")
@RestController
public class SysPostController {

    @Autowired
    SysPostService sysPostService;

    @GetMapping("/find")
    public R<Page> findC(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "2") Integer size,
            @RequestParam String postCode,
            @RequestParam String postName,
            @RequestParam String status
    ){
        //创建分页对象
        Page<SysPost> page = new Page<>(current,size);
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<>();
        if (postCode!=null){
            queryWrapper.lambda().like(SysPost::getPostCode,postCode);
        }
        if (postName!=null){
            queryWrapper.lambda().like(SysPost::getPostName,postName);
        }
        if (status!=null && !status.equals("")){
            queryWrapper.lambda().eq(SysPost::getStatus,status);
        }
        //根据分页对象，查询分页数据
        page = sysPostService.page(page,queryWrapper);
        return R.success(page);
    }

    @PostMapping("/insert")
    public R<String> insert(@RequestBody SysPost sysPost,@RequestAttribute("user") SysUser users){
        sysPost.setCreateBy(users.getUsername());
        sysPost.setCreateTime(new Date());
        sysPostService.save(sysPost);
        return R.success("添加成功");
    }

    @PutMapping("/update")
    public R<String> update(@RequestBody SysPost sysPost,@RequestAttribute("user") SysUser users){
        sysPost.setUpdateBy(users.getUsername());
        sysPost.setUpdateTime(new Date());
        sysPostService.updateById(sysPost);
        return R.success("修改成功");
    }

    @DeleteMapping("/delete/{postId}")
    public R<String> delete(@PathVariable("postId") Integer postId){
        sysPostService.removeById(postId);//单行删除
        return R.success("删除成功");
    }

    @DeleteMapping("/deleteT")
    public R<String> deleteT(@RequestBody List<Long> postId){
        sysPostService.removeByIds(postId);//多选删除
        return R.success("删除成功");
    }

}
